// import { ElMessageBox, messageType, } from "element-plus";
import { AnyFunc } from "./type";
import Reflect from "@/utils/reflect";

export type MessageType = "error" | "warning" | "info" | "success";

export function ConfirmIntercept<T>(message: string, type?: MessageType, judgeShow?: (this: T) => boolean) {
  return function (target: T, propertyKey: any, descriptor: TypedPropertyDescriptor<AnyFunc>) {
    Reflect.setValue(target, ["ConfirmIntercept", propertyKey], descriptor.value);
    descriptor.value = async function (...args) {
      let isPass: boolean = true;
      try {
        if (!judgeShow || judgeShow.call(this, message, type)) {
          // await ElMessageBox.confirm(
          //   `此操作将${message}, 是否继续？`,
          //   "提示",
          //   {
          //     type: type || "warning",
          //     confirmButtonClass: "el-button--primary--fill",
          //   }
          // );
          if (!confirm(`此操作将${message}, 是否继续？`)) {
            throw new Error("cancel");
          }
        }
      } catch (error) {
        isPass = false;
      }
      if (isPass) {
        await Reflect.getValue<AnyFunc>(target, ["ConfirmIntercept", propertyKey]).call(this, ...args);
      }
    };
  };
}
